class Node:
    def __init__(self,val=0,next=None):
        self.val=val 
        self.next=next 

def build(head,pos):
    head=[Node(i) for i in head]
    cur=head[0]
    for i in range(len(head)-1):
        cur.next=head[i+1]
        cur=cur.next 
    if pos>=0: cur.next=head[pos]
    return head[0]

def solve(head):
    slow=head
    if not slow: return False
    fast=head.next
    if not fast: return False 
    while fast:
        if fast==slow: return True 
        fast=fast.next
        if not fast: return False 
        fast=fast.next
        slow=slow.next 
    return False 

head=build([3,2,0,-4],1)
print(solve(head))
head=build([1,2],0)
print(solve(head))
head=build([1],-1)
print(solve(head))

